package com.masterclient.app.businessmodule.homemodule.view.adapter.diamond;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.commonbean.banner.BannerNewItemBean;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.homemodule.bean.banner.BannerNewBean;
import com.masterclient.app.businessmodule.homemodule.view.holder.HomeViewHolder;
import com.masterclient.app.businessmodule.homemodule.old.EventAdDataCallback;
import com.masterclient.app.utils.CommonUtil;
import com.eoner.baselibrary.utils.UtilCollection;

import java.util.List;


public class HomePageXDiamondZoneAdapter extends DelegateAdapter.Adapter<HomeViewHolder> {

    private Context mContext;
    private LayoutHelper mLayoutHelper;
    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private int mCount = 0;
    private BannerNewBean shCategoryData;
    private EventAdDataCallback eventAdDataCallback;

    public void setEventAdDataCallback(EventAdDataCallback eventAdDataCallback) {
        this.eventAdDataCallback = eventAdDataCallback;
    }

    public HomePageXDiamondZoneAdapter(Context context, LayoutHelper layoutHelper, int count) {
        this(context, layoutHelper, count, null, null);
    }

    public HomePageXDiamondZoneAdapter(Context context, LayoutHelper layoutHelper, int count, @NonNull VirtualLayoutManager.LayoutParams layoutParams, BannerNewBean shCategoryData) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.mCount = count;
        this.mLayoutParams = layoutParams;
        this.shCategoryData = shCategoryData;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @Override
    protected void onBindViewHolderWithOffset(HomeViewHolder holder, int position, int offsetTotal) {
    }

    @Override
    public HomeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    public void onBindViewHolder(HomeViewHolder holder, int position) {
        if (mLayoutParams != null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }

        RecyclerView recyclerView = holder.itemView.findViewById(R.id.diamond_zone_recycler);

        //金刚区
        List<BannerNewItemBean> shCategoryBeans = shCategoryData.getList();
        if (shCategoryData != null && shCategoryBeans != null) {
            recyclerView.setVisibility(View.VISIBLE);
            int height;
            if (shCategoryData == null || shCategoryData.getBg_image() == null || TextUtils.isEmpty(shCategoryData.getBg_image().getUrl())) {
                height = ScreenUtils.getScreenWidth() * 172 / 750;
                String color = shCategoryData.getBg_color();
                if (UtilCollection.isColor(color)) {
                    recyclerView.setBackgroundColor(Color.parseColor(color));
                } else {
                    recyclerView.setBackgroundColor(Color.parseColor("#FFFFFF"));
                }
            } else {
                height = ScreenUtils.getScreenWidth() * Integer.parseInt(shCategoryData.getBg_image().getHeight()) / Integer.parseInt(shCategoryData.getBg_image().getWidth());
                height = height / ((int) Math.ceil(shCategoryBeans.size() / 4f));
            }

            recyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
            DiamondZoneAdapter diamondZoneAdapter = new DiamondZoneAdapter(shCategoryBeans, height);
            diamondZoneAdapter.bindToRecyclerView(recyclerView);
            diamondZoneAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    BannerNewItemBean categoryBean = diamondZoneAdapter.getData().get(position);
                    eventAdDataCallback.adData(categoryBean.getS_ad_name(), categoryBean.getS_ad_id(), categoryBean.getS_group_name(), categoryBean.getS_group_id(),
                            categoryBean.getTarget(), categoryBean.getTarget_id(), String.valueOf(position));
                    CommonUtil.goNewAnyWhere(mContext, categoryBean.getTarget_platform(), categoryBean.getTarget_message(), categoryBean.getTarget(), categoryBean.getTarget_id(), categoryBean.getImage(), categoryBean.getS_ad_name(), categoryBean.getS_ad_id());
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mCount;
    }

    @Override
    public int getItemViewType(int position) {
        return 4;
    }
}
